package com.wetao.module_report.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.wetao.module_report.R;
import com.wetao.module_report.R2;
import com.wetao.module_report.ReportStageDetailActivity;
import com.wetao.module_report.adapter.ReportMain_h_Adapter;
import com.google.gson.reflect.TypeToken;
import com.wetao.lib_common.ConstantArouter;
import com.wetao.lib_common.Constants;
import com.wetao.lib_common.base.BaseNestingLazyFragment;
import com.wetao.lib_common.bean.response.GetSubjectList;
import com.wetao.lib_common.contract.EmptyCon;
import com.wetao.lib_common.presenter.EmptyPre;
import com.wetao.lib_common.utils.JsonUtils;
import com.wetao.lib_common.utils.LogUtils;
import com.wetao.lib_common.utils.MultiClickUtil;
import com.wetao.lib_common.utils.MyDividerItemDecoration;
import com.wetao.lib_common.utils.SPUtils;
import com.wetao.lib_common.utils.WrapContentLinearLayoutManager;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class ReportStageFragment extends BaseNestingLazyFragment<EmptyCon.View, EmptyPre>
        implements EmptyCon.View  {

    private static final String ARG_PARAM1 = "param1";
    private OnFragmentInteractionListener mListener;

    List<GetSubjectList> mSubjectList;
    GetSubjectList mCurSubject;
    @BindView(R2.id.rv_h)
    RecyclerView rv_h;
    private ReportMain_h_Adapter mReportMain_h_Adapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private MyDividerItemDecoration mDividerItemDecoration;
    private int mCurSubjectPosition = 0;
    private String studentId;

    @BindView(R2.id.ll_day)
    LinearLayout ll_day;
    @BindView(R2.id.ll_week)
    LinearLayout ll_week;
    @BindView(R2.id.ll_month)
    LinearLayout ll_month;
    @BindView(R2.id.tv_title_day)
    TextView tv_title_day;
    @BindView(R2.id.tv_title_week)
    TextView tv_title_week;
    @BindView(R2.id.tv_title_month)
    TextView tv_title_month;

    public ReportStageFragment() {
    }

    public static ReportStageFragment newInstance() {
        ReportStageFragment fragment = new ReportStageFragment();
        /*Bundle args = new Bundle();
        args.putBoolean(ARG_PARAM1, param1);
        fragment.setArguments(args);*/
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.report_stage_fragment;
    }

    @Override
    public EmptyPre createPresenter() {
        return new EmptyPre(mContext);
    }

    @Override
    public EmptyCon.View createView() {
        return this;
    }



    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        LogUtils.e("onAttach");
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        LogUtils.e("onDetach");
        mListener = null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    @Override
    public void onFragmentFirst() {
        super.onFragmentFirst();
    }

    @Override
    public void onFragmentVisble() {
        super.onFragmentVisble();

    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onFragmentInVisible() {
        super.onFragmentInVisible();
    }


    @Override
    public void init(View rootView) {
        /*if (getArguments() != null) {
            isYes = getArguments().getBoolean(ARG_PARAM1);
        }*/

        initRecyclerView();
        String str = SPUtils.getInstance().getString(Constants.GET_SUBJECT_LIST);
        studentId = SPUtils.getInstance().getString(Constants.USER_ID_KEY);
        mSubjectList = JsonUtils.fromJsonList(str, new TypeToken<List<GetSubjectList>>() {}.getType());

        if( mSubjectList.size() > 0) {
            mReportMain_h_Adapter.setmCurPosition(0);
            mReportMain_h_Adapter.updateData(mSubjectList);
            mCurSubject = mSubjectList.get(0);
            refreshUI();
        }else{
            ll_day.setVisibility(View.GONE);
            ll_week.setVisibility(View.GONE);
            ll_month.setVisibility(View.GONE);
        }
    }

    private void refreshUI() {
        tv_title_day.setText(mCurSubject.getSubjectName() + "学情-日报");
        tv_title_week.setText(mCurSubject.getSubjectName() + "学情-周报");
        tv_title_month.setText(mCurSubject.getSubjectName() + "学情-月报");
    }

    private void initRecyclerView() {
        mLayoutManager = new WrapContentLinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        mReportMain_h_Adapter = new ReportMain_h_Adapter(mContext);
        rv_h.setLayoutManager(mLayoutManager);
        mDividerItemDecoration = new MyDividerItemDecoration(getActivity(), LinearLayoutManager.HORIZONTAL, R.drawable.divider_mileage_voice_class);
        //rv_h.addItemDecoration(mDividerItemDecoration);
        rv_h.setItemAnimator( new DefaultItemAnimator());
        rv_h.setAdapter(mReportMain_h_Adapter);
        ((ReportMain_h_Adapter)mReportMain_h_Adapter).setOnItemClickListener(new ReportMain_h_Adapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                LogUtils.i("onItemClick--" + position);
                mCurSubjectPosition = position;
                mCurSubject = mSubjectList.get(position);
                refreshUI();
            }
        });
    }

    @OnClick({R2.id.ll_day,
            R2.id.ll_week,
            R2.id.ll_month
    })
    public void onClickView(View v) {
        int id = v.getId();
        if(!MultiClickUtil.isFastClick()) {
            return;
        }
        int type = ReportStageDetailActivity.STAGE_TYPE_DAY;
        String typeStr = ReportStageDetailActivity.STAGE_TYPE_DAY_STR;
        if(id == R.id.ll_day) {
            type = ReportStageDetailActivity.STAGE_TYPE_DAY;
            typeStr = ReportStageDetailActivity.STAGE_TYPE_DAY_STR;
        } else if(id == R.id.ll_week) {
            type = ReportStageDetailActivity.STAGE_TYPE_WEEK;
            typeStr = ReportStageDetailActivity.STAGE_TYPE_WEEK_STR;
        } else if(id == R.id.ll_month) {
            type = ReportStageDetailActivity.STAGE_TYPE_MONTH;
            typeStr = ReportStageDetailActivity.STAGE_TYPE_MONTH_STR;
        }

        ARouter.getInstance().build(ConstantArouter.PATH_REPORT_STAGE_DETAIL_ACTIVITY)
                .withInt("type", type)
                .withString("typeStr", typeStr)
                .withInt("subjectId", mCurSubject.getSubjectId())
                .withString("subjectName", mCurSubject.getSubjectName())
                .navigation();
    }

    public interface OnFragmentInteractionListener {

    }
}
